import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rock extends Trap
{
    private int yVel=0;
    private boolean exists;
    private Timer spawnTimer=new Timer();
    private CollisionBox colDown=new CollisionBox(this,0,30,10,10);
    private boolean addedToWorld=false;

    public Rock(int x,int del, int iniD,int skin)
    {   
        super(x,del,iniD);
        
        exists=false;
        
        damage=10;
        
        GreenfootImage image;
        
        switch(skin)
        {
            case 1:     image=new GreenfootImage("rock_jungle1.png");
                        break;
            case 2:     image=new GreenfootImage("rock_jungle2.png");
                        break;
            default:    image=new GreenfootImage("rock.png");
                        break;
        }
        
        
        image.setTransparency(0);
        setImage(image);
       
        setRotation(Greenfoot.getRandomNumber(360));
    }
    
    public void act() 
    {
        timer.tick();
        spawnTimer.tick();
        activeTimer.tick();
        
        GameWorld world=(GameWorld)getWorld();
        
        if(addedToWorld==false)
        {
            addedToWorld=true;
            world.addObject(colDown,getX(),getY());
        }
        
        
        int scrollx=world.getScrollx();
        
        setLocation(scrollx+posX+360,getY());
        
        if(active==true || !activeTimer.wait(initialDelay))     
        activeImage();
        if(active==false)
        return;
        
        if(exists==false && !spawnTimer.wait(delay))
        exists=true;
        if(exists==false)
        return;
        
        /*if(getImage().getTransparency()==0)
        {
            GreenfootImage image=getImage();
            image.setTransparency(255);
            setImage(image);
        }*/
        
        Player player=getPlayer();
        
        if(isTouchingPlayer() && !colDown.isTouchingGround())
        player.getHurt(damage);
        
        if(!colDown.isTouchingGround() && getY()<world.getHeight()+50)
        fall();
        else
        disappear();
        
    }   
    
    public void fall()
    {
        yVel+=1;
        setLocation(getX(),getY()+yVel);
        
        if(!timer.wait(50))
        turn(10+Greenfoot.getRandomNumber(30));
    }
    
    public void disappear()
    {
        int scrollx=((GameWorld)getWorld()).getScrollx();
        
        GreenfootImage image=getImage();
        
        if(image.getTransparency()>50)
        {
            if(!timer.wait(100))
            image.setTransparency(image.getTransparency()-50);
        }else  
        {
            image.setTransparency(0);
            setLocation(scrollx+posX+getWorld().getWidth()/2,-10);
            exists=false;
            yVel=0;
        }
        
        setImage(image);
    }
}
